package com.superbytecode.stencil.enums;

import com.superbytecode.stencil.utils.StringUtil;
import lombok.Getter;

import java.util.Arrays;

/**
 * All rights Reserved, Designed By www.super-master.com
 *
 * @ProjectName: code-stencil
 * @Package: com.superbytecode.stencil.definitions
 * @ClassName: MyBatisTypeEnum
 * @Description: []
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2024/10/11 20:08
 * @Version: V1.0
 * @Copyright: 2024 www.super-master.cn Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
@Getter
public enum MyBatisTypeEnum {
    BIT("bit", "BOOLEAN"),//
    BOOLEAN("boolean", "BOOLEAN"),//
    TINYINT("tinyint", "TINYINT"),//
    SMALLINT("smallint", "INTEGER"),//
    INT("int", "INTEGER"),//
    BIGINT("bigint", "LONG"),//
    FLOAT("float", "FLOAT"),//
    DOUBLE("double", "DOUBLE"),//
    DECIMAL("decimal", "DECIMAL"),//
    VARCHAR("varchar", "VARCHAR"),//
    DATETIME("datetime", "TIMESTAMP"),//
    BLOB("blob", "BLOB"),
    ;
    /**
     * 数据类型分类
     * @see: {@link JavaTypeEnum}#category
     * @see: {@link SQLTypeEnum}#category
     */
    final private String category;
    /**
     * mybatis属性列类型
     */
    final private String fieldType;
    // -----------------------------------------------------------------------------------------------------------------

    /**
     * 解析数据库column类型对应的mybatis列类型
     *
     * @param sqlType 数据列定义sql类型
     * @return mybatis列类型
     */
    public static MyBatisTypeEnum ofMybatisType(String sqlType) {
        return Arrays.stream(values())//
                .filter(v -> StringUtil.equalsIgnoreCase(v.getCategory(), sqlType))// 匹配原始入参
                .findAny() // 获取对应的类型，维护获取到则异常处理
                .orElseThrow(() -> new IllegalArgumentException("Mybatis no match column type: " + sqlType));
    }

    // -----------------------------------------------------------------------------------------------------------------
    MyBatisTypeEnum(String category, String fieldType) {
        this.category = category;
        this.fieldType = fieldType;
    }
}